Guided Practice 8.4

All we need to do is to redefine successors. For example, we could write

;; Node Graph -> ListOfNode
;; STRATEGY: Struct Decomp on g : Graph 
(define (successors n1 g)
  (cond
    [(empty? g) empty]
    [else (if (node=? (node-info-node (first g)) n1)
              (node-info-successors (first g))
              (successors n1 (rest g)))]))

;; NodeInfo -> Node
;; SD on i : NodeInfo
(define (node-info-node i)
  (first i))

;; NodeInfo -> ListOfNode
;; SD on i : NodeInfo
(define  (node-info-successors i)
  (rest i))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Of course, these help functions are very simple, so we might inline
;; them and write

;; Node Graph -> ListOfNode
;; STRATEGY: Struct Decomp on g : Graph, (first g) : NodeInfo
(define (successors n1 g)
  (cond
    [(empty? g) empty]
    [else (if (node=? (first (first g)) n1)
              (rest (first g))
              (successors n1 (rest g)))]))

;; But this is double decomposition.

Do you think the second version is clearer or less clear than the first one? If the person you who followed you in your job had to maintain this code, how hard would it be for him or her to figure out the meaning of (first (first g)) ? What could be done to improve this code?


Last modified: Sun Oct 19 17:27:04 Eastern Daylight Time 2014